package com.adong.rabbitmq.util;

import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class ConnectionUtil {
    public static Connection getConnection() {
        // 创建连接工厂
        ConnectionFactory connectionFactory = new ConnectionFactory();
        // 主机地址,默认localhost
        connectionFactory.setHost("192.168.11.110");
        // 连接端口,默认5672
        connectionFactory.setPort(5672);
        // 虚拟主机名称;默认为/
        connectionFactory.setVirtualHost("/test");
        // 连接用户,默认guest
        connectionFactory.setUsername("adong");
        // 连接密码,默认guest
        connectionFactory.setPassword("962464");
        // 创建连接
        Connection connection = null;
        try {
            connection = connectionFactory.newConnection();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }

        return connection;
    }
}
